package com.xbdun.service.bmp.service.system.impl;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xbdun.common.api.Client;
import com.xbdun.dao.bmp.mapper.OrleDepartMapper;
import com.xbdun.dao.bmp.mapper.PowerRoleDepartMapper;
import com.xbdun.dao.bmp.mapper.PowerUserMenuMapper;
import com.xbdun.facade.bmp.exceptions.BmpBizException;
import com.xbdun.facade.bmp.request.system.ModifyDepartRoleRequest;
import com.xbdun.service.bmp.service.system.PowerRoleDepartService;

/**
* @ClassName: PowerRoleDepartServiceImpl
* @Description 新增/修改部门与角色的权限
* @author zhanggt 360730440@qq.com
* @date 2017年4月26日 下午3:04:29
*/
@Service
public class PowerRoleDepartServiceImpl implements PowerRoleDepartService{

	@Autowired
	PowerRoleDepartMapper powerRoleDepartMapper;
	@Autowired
	OrleDepartMapper orleDepartMapper;
	@Autowired
	PowerUserMenuMapper powerUserMenuMapper;
	
	@Transactional
	public void mofidyDepartRole(ModifyDepartRoleRequest request, Client client)throws Exception{
		//判断是否存在部门关联角色
		if(0==orleDepartMapper.existDepartRoleId(request.getOrleDepartId(), client.getOrgCode())){
			throw BmpBizException.DB_SELECTONE_IS_NULL.newInstance("对不起，请输入正确的部门角色ID");
		}
		//新增
		if(CollectionUtils.isNotEmpty(request.getAddMenuCode())){
			powerRoleDepartMapper.addPowerRolesDepart(client.getOrgCode(), request.getOrleDepartId(), request.getAddMenuCode());
		}
		//删除
		if(CollectionUtils.isNotEmpty(request.getDelMenuCode())){
			//删除员工授权设置
			powerUserMenuMapper.delOrgByMenu(client.getOrgCode(),request.getOrleDepartId(),request.getDelMenuCode());
			//删除部门关联角色权限
			powerRoleDepartMapper.delPowerRolesDepart(client.getOrgCode(),request.getOrleDepartId(), request.getDelMenuCode());
		}
	}

	public List<String> queryOrleDepartMenus(Long orleDepartId, Client client) {
		
		return powerRoleDepartMapper.queryOrleDepartMenus(orleDepartId,client.getOrgCode());
	}
}
